/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.superruzafa.codingstandardvalidator.ui;

import java.awt.Cursor;
import java.io.File;
import java.util.prefs.Preferences;
import javax.swing.DefaultComboBoxModel;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.util.NbPreferences;
import org.superruzafa.codingstandardvalidator.CodingStandardValidatorException;
import org.superruzafa.codingstandardvalidator.codesniffer.CodeSniffer;

final class CodingStandardValidatorSettingsPanel extends javax.swing.JPanel {

    private final CodingStandardValidatorSettingsOptionsPanelController controller;
    private final Preferences preferences;

    CodingStandardValidatorSettingsPanel(CodingStandardValidatorSettingsOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        preferences = NbPreferences.forModule(getClass());
        // TODO listen to changes in form fields and call controller.changed()
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        codeSnifferScript = new javax.swing.JTextField();
        browseCodeSnifferScript = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        codingStandard = new javax.swing.JComboBox();
        autodetectInstalledCodingStandards = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        reportTypes = new javax.swing.JComboBox();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.jLabel1.text")); // NOI18N

        codeSnifferScript.setText(org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.codeSnifferScript.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(browseCodeSnifferScript, org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.browseCodeSnifferScript.text")); // NOI18N
        browseCodeSnifferScript.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseCodeSnifferScriptActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.jLabel2.text")); // NOI18N

        codingStandard.setEditable(true);

        org.openide.awt.Mnemonics.setLocalizedText(autodetectInstalledCodingStandards, org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.autodetectInstalledCodingStandards.text")); // NOI18N
        autodetectInstalledCodingStandards.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autodetectInstalledCodingStandardsActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(CodingStandardValidatorSettingsPanel.class, "CodingStandardValidatorSettingsPanel.jLabel3.text")); // NOI18N

        reportTypes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Default", "Full", "Xml" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(reportTypes, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(codingStandard, 0, 224, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(autodetectInstalledCodingStandards))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(codeSnifferScript, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(browseCodeSnifferScript)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(codeSnifferScript, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseCodeSnifferScript))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codingStandard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(autodetectInstalledCodingStandards)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reportTypes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(203, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void browseCodeSnifferScriptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseCodeSnifferScriptActionPerformed
        FileChooserBuilder fileChooser = new FileChooserBuilder(getClass());
        File file = fileChooser.showOpenDialog();
        if (file != null) {
            codeSnifferScript.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_browseCodeSnifferScriptActionPerformed

    private void autodetectInstalledCodingStandardsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autodetectInstalledCodingStandardsActionPerformed
        codingStandard.removeAllItems();
        CodeSniffer codeSniffer = new CodeSniffer();
        codeSniffer.setScriptPath(codeSnifferScript.getText());
        boolean loaded = false;
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            loaded = codeSniffer.loadInstalledCodingStandards();
        } catch (CodingStandardValidatorException ex) {
            loaded = false;
        }
        setCursor(Cursor.getDefaultCursor());

        if (loaded) {
            DefaultComboBoxModel model = (DefaultComboBoxModel) codingStandard.getModel();
            for (String cs : codeSniffer.getInstalledCodingStandards()) {
                model.addElement(cs);
            }
        } else {
            NotifyDescriptor nd = new NotifyDescriptor.Message("A problem occurred while trying to autodetect the installed coding standards.\nPlease, be sure you have specified the correct path for the Code Sniffer script.", NotifyDescriptor.WARNING_MESSAGE);
            DialogDisplayer.getDefault().notify(nd);
        }
    }//GEN-LAST:event_autodetectInstalledCodingStandardsActionPerformed

    void load() {
        codeSnifferScript.setText(preferences.get("code-sniffer-script", ""));
        codingStandard.setSelectedItem(preferences.get("coding-standard", ""));

        DefaultComboBoxModel model = (DefaultComboBoxModel) reportTypes.getModel();
        int index = model.getIndexOf(preferences.get("report-type", ""));
        if (index < 0) {
            index = 0;
        }
        reportTypes.setSelectedIndex(index);
    }

    void store() {
        preferences.put("code-sniffer-script", codeSnifferScript.getText());
        preferences.put("coding-standard", (codingStandard.getSelectedItem() != null)
                ? codingStandard.getSelectedItem().toString()
                : "");
        preferences.put("report-type", reportTypes.getSelectedItem().toString());
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton autodetectInstalledCodingStandards;
    private javax.swing.JButton browseCodeSnifferScript;
    private javax.swing.JTextField codeSnifferScript;
    private javax.swing.JComboBox codingStandard;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox reportTypes;
    // End of variables declaration//GEN-END:variables
}
